import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import Pages from "vite-plugin-pages"
import { resolve } from "path"
export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "src/assets/css/global.scss" as *;`,
        charset: false
      }
    }
  },
  plugins: [
    vue(),
    AutoImport({
      imports: ["vue", "vue-router", "vuex"], // 自动导入composition api
      dts: "src/auto-import.d.ts", // 生成 `auto-import.d.ts` 全局声明
      resolvers: [
        ElementPlusResolver({
          importStyle: "sass"
        })
      ]
    }),
    Components({
      dirs: ["src/components"], // 要导入组件的目录路径
      deep: true, // 搜索子目录
      dts: "src/components/components.d.ts", // 生成 `components.d.ts` 全局声明
      resolvers: [
        ElementPlusResolver({
          importStyle: "sass"
        })
      ]
    }),
    Pages({
      // 自动读取src/views下的vue文件，生成路由信息，默认路由路径'/‘
      dirs: [{ dir: "src/views", baseRoute: "/" }],
      // 异步方式加载路由组件
      importMode: "async",
      // 遍历路由信息，给默认路由加一个redirect
      extendRoute(route) {
        // if (route.path === "/") return { ...route, redirect: "login" }
      }
    })
  ],
  resolve: {
    alias: {
      "@": resolve("./src"),
      "@utils": resolve("./src/utils")
    }
  },
  server: {
    open: true // 是否自动打开浏览器
  }
})
